package com.stx.openeyes.view.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.stx.openeyes.R;
import com.stx.openeyes.SealConst;
import com.stx.openeyes.dto.Response;
import com.stx.openeyes.model.User;
import com.stx.openeyes.utils.ContextUtil;
import com.stx.openeyes.utils.HttpAdress;
import com.stx.openeyes.utils.NToast;
import com.stx.openeyes.widget.ClearWriteEditText;
import com.stx.openeyes.widget.LoadDialog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

public class UpdatePasswordActivity extends AppCompatActivity implements View.OnClickListener {
    private final static String TAG = "UpdatePasswordActivity";
    private static final int CHECK_PHONE = 1;
    private static final int SEND_CODE = 2;
    private static final int VERIFY_CODE = 3;
    private static final int REGISTER = 4;
    private static final int REGISTER_BACK = 1001;
    private ImageView mImgBackground;
    private ClearWriteEditText mPhoneEdit, mNickEdit, mPasswordEdit, mPasswordEdit2;
    private Button mConfirm;
    private String mPhone, mNickName, mPassword, mPassword2;
    protected Context mContext;
    private Gson mGson;

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_update_password);
            mContext = this;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        initView();

    }

    private void initView() {
        mPhoneEdit = (ClearWriteEditText) findViewById(R.id.upd_phone);
        mPasswordEdit = (ClearWriteEditText) findViewById(R.id.upd_password);
        mPasswordEdit2 = (ClearWriteEditText) findViewById(R.id.upd_password2);
        mConfirm = (Button) findViewById(R.id.upd_button);

        mConfirm.setOnClickListener(this);

//        TextView goLogin = (TextView) findViewById(R.id.reg_login);
//        goLogin.setOnClickListener(this);

        mImgBackground = (ImageView) findViewById(R.id.upd_img_backgroud);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Animation animation = AnimationUtils.loadAnimation(UpdatePasswordActivity.this, R.anim.translate_anim);
                mImgBackground.startAnimation(animation);
            }
        }, 10);

        addEditTextListener();
    }

    private void addEditTextListener() {
        mPhoneEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mPasswordEdit2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 5) {
                    mConfirm.setClickable(true);
                    mConfirm.setBackgroundDrawable(getResources().getDrawable(R.drawable.rs_select_btn_blue));
                } else {
                    mConfirm.setClickable(false);
                    mConfirm.setBackgroundDrawable(getResources().getDrawable(R.drawable.rs_select_btn_gray));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.reg_login:
//                finish();
//                //startActivity(new Intent(this, LoginActivity.class));
//                break;
            case R.id.upd_button:
                mPhone = mPhoneEdit.getText().toString().trim();
                mPassword = mPasswordEdit.getText().toString().trim();
                mPassword2 = mPasswordEdit2.getText().toString().trim();


                if (TextUtils.isEmpty(mPhone)) {
                    NToast.shortToast(mContext, getString(R.string.phone_number_is_null));
                    mPhoneEdit.setShakeAnimation();
                    return;
                }
                if (TextUtils.isEmpty(mPassword)) {
                    NToast.shortToast(mContext, getString(R.string.password_is_null));
                    mPasswordEdit.setShakeAnimation();
                    return;
                }
                if (TextUtils.isEmpty(mPassword2)) {
                    NToast.shortToast(mContext, "确认密码不能为空");
                    mPasswordEdit2.setShakeAnimation();
                    return;
                }
                if (mPassword.contains(" ")) {
                    NToast.shortToast(mContext, getString(R.string.password_cannot_contain_spaces));
                    mPasswordEdit.setShakeAnimation();
                    return;
                }
                if(!mPassword.equals(mPassword2)){
                    NToast.shortToast(mContext, "两次输入密码须一致");
                    return;
                }
                LoadDialog.show(mContext);
                requestRegister(mPhone, mPassword);
                break;
        }
    }

    @SuppressLint("LongLogTag")
    private void requestRegister(String mobile, String password) {

        mGson = new Gson();


        OkHttpUtils.post()
                .url(HttpAdress.UPDATE_PASSWORD)
                .addHeader("token", ContextUtil.getCurrentUserToken())
                .addParams("newPassword", password)
                .build()
                .execute(new StringCallback() {
                    @Override
                        public void onError(Call call, Exception e, int id) {
                            Log.i("===register error", e.getMessage());
                            NToast.shortToast(mContext, SealConst.NETWORK_ERROR);
                            LoadDialog.dismiss(mContext);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("===reg response json", response);
                        mGson = new Gson();
                        Response<User> loginResp = mGson.fromJson(response, Response.class);
                        if (!loginResp.isSuccess()) {
                            NToast.shortToast(mContext, loginResp.getMessage());
                            LoadDialog.dismiss(mContext);
                            return;
                        }
                        LoadDialog.dismiss(mContext);

                        SharedPreferences settings = getSharedPreferences(SealConst.USER_INFO, 0);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putString(SealConst.USER_INFO, mGson.toJson(loginResp.getData()));
                        editor.commit();
                        startActivity(new Intent(mContext, LoginActivity.class));
                    }
                });
    }
}
